﻿using DryIoc;
using MaterialDesignThemes.Wpf;
using StarryEdge.Core;
using StarryEdge.Core.Enums;
using StarryEdge.ViewModels.Dialog;
using StarryEdge.Views.Dialog;
using StarryEdge.WPF.Lang;
using StarryEdge.WPF.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace StarryEdge.Domains
{
    public class DialogStarryEdge : IDialogHost
    {
        private Func<string, LogLevel, bool, PopUpTypes, DialogShowButton, Task<object>> _messageDialogs = null;
        private Func<string,string,string,string,Task<object>> _mesDialog = null;

        public DialogStarryEdge()
        {
            _messageDialogs = new Func<string, LogLevel, bool, PopUpTypes, DialogShowButton, Task<object>>(async (str, levelType, isOper, host, showButton) =>
            {
                IsOpenDialog(host);
                string title = LangManager.Instance.Lang["Info"];
                switch (levelType)
                {
                    case LogLevel.Error:
                        title = LangManager.Instance.Lang["Error"];
                        break;
                    case LogLevel.Info:
                        title = LangManager.Instance.Lang["Info"];
                        break;
                    case LogLevel.Warning:
                        title = LangManager.Instance.Lang["Warning"];
                        break;
                    case LogLevel.Success:
                        title = LangManager.Instance.Lang["Success"];
                        break;
                }
                return await DialogHost.Show(new MessagePopup()
                {
                    DataContext = new MessagePopupDialog(title, str, showButton, levelType)
                }, host.ToString());
            });

            _mesDialog = new Func<string, string, string, string, Task<object>>((interfaceName, url, messageEncoding, messageContent) =>
            {
                IsOpenDialog(PopUpTypes.UserWindow);
                DialogHost.Show(new MESMessageShow()
                {
                    DataContext = new MESMessageShowPopupDialog(interfaceName, url, messageEncoding, messageContent)
                }, PopUpTypes.UserWindow.ToString());
                return default;
            });
        }

        public Task<object> DialogShow(string message, LogLevel logLevel, bool isOperation = false, PopUpTypes hostDialog = PopUpTypes.UserWindow, DialogShowButton dialogShowButton = DialogShowButton.None)
        {
            return ThreadHelper.IsUIThread(() => _messageDialogs.Invoke(message, logLevel, isOperation, hostDialog, dialogShowButton),
                () => ThreadHelper.CrossThreadAsync(sender => _messageDialogs.Invoke(message, logLevel, isOperation, hostDialog, dialogShowButton), (object)1));
        }

        public void MesShow(string interfaceName, string url, string code, string message)
        {
            ThreadHelper.IsUIThread(() => _mesDialog.Invoke(interfaceName, url, code, message),
                () => ThreadHelper.CrossThreadAsync(sender => _mesDialog.Invoke(interfaceName, url, code, message), (object)1));
        }

        private void IsOpenDialog(PopUpTypes hostDialog)
        {
            if (DialogHost.IsDialogOpen(hostDialog.ToString())) DialogHost.Close(hostDialog.ToString());
        }
    }
}
